package com.cpdmali.courrier.dao.imp;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Repository;

import com.cpdmali.courrier.dao.DomainsDao;
import com.cpdmali.courrier.entities.Domains;

@Repository
public class DomainsDaoImpl implements DomainsDao {
	
	private static final Logger log = LoggerFactory.getLogger(DomainsDaoImpl.class);
	
	@Autowired
	private SessionFactory sessionFactory;
		
	private Session getCurrentSession() {
		return sessionFactory.getCurrentSession();
	}
	
	
	public void persist(Domains transientInstance) {
		log.debug("Saving Domains instance");
		try {
			getCurrentSession().save(transientInstance);
			log.debug("save Domains succefull");
		} catch (RuntimeException e) {
			log.debug("save Domains failed", e);
		}		
	}

	public void remove(Domains persistentInstance) {
		log.debug("Remove Domains instance");
		try {
			if (persistentInstance != null) {
				getCurrentSession().delete(persistentInstance);
			} else {
				log.debug("Removing Domains instance");
				throw new ClassNotFoundException("Parameter null");
			}
		} catch (RuntimeException e) {
			log.debug("Remove Domains failed", e);
		} catch (ClassNotFoundException e) {
			log.debug("Remove Domains failed because the attribut is null", e);
		}		
	}

	public Domains merge(Domains detachedInstance) {
		log.debug("Update Domains instance");
		try {
			if (detachedInstance != null) {
				getCurrentSession().update(detachedInstance);
				return findById(detachedInstance.getDomain());
			} else {
				log.debug("Updating Domains instance");
				throw new ClassNotFoundException("Parameter null");
			}
		} catch (RuntimeException e) {
			log.debug("Update Domains failed", e);
		} catch (ClassNotFoundException e) {
			log.debug("Update Domains failed because the attribut is null", e);
		}
		return null;
	}

	public Domains findById(String id) {
		log.debug("Find CvAffectation instance by Id :" + id);
		try {
			if ("".equals(id)) {
				log.debug("Find CvAffectation by id failed because parameter id null");
				throw new IllegalArgumentException("Parameter is null");
			}
			Domains domains = (Domains) getCurrentSession().get(Domains.class, id);
			return domains;
		} catch (RuntimeException e) {
			log.debug("Find Domains by id failed", e);
		}
		return null;
	}

	public List<Domains> findAll() {
		log.debug("Find all Domains ");
		try {
			return getCurrentSession().createCriteria(Domains.class).list();
		} catch (RuntimeException e) {
			log.debug("Find all Domains failed", e);
		}
		return null;
	}

	public List<Domains> findAllLimit(int limit) {
		log.debug("Find all Domains ");
		try {
			List<Domains> domains = getCurrentSession().createCriteria(Domains.class)
																.setFirstResult(0)
																.setMaxResults(limit)
																.list();
			if(domains==null){
				return new ArrayList<Domains>();
			}
			return domains;
		} catch (RuntimeException e) {
			log.debug("Find all Domains failed", e);
		}
		return null;
	}

}
